package com.example.dormmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormmanage.dao.IMoveDao;
import com.example.dormmanage.dao.IRoomDao;
import com.example.dormmanage.dao.IStudentDao;
import com.example.dormmanage.entity.Move;
import com.example.dormmanage.entity.Room;
import com.example.dormmanage.entity.Student;
import com.example.dormmanage.service.IMoveService;
import com.example.dormmanage.util.BaseContext;
import com.example.dormmanage.view.MoveDto;
import com.example.dormmanage.view.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MoveService extends ServiceImpl<IMoveDao, Move> implements IMoveService {

    @Autowired
    private IStudentDao studentDao;

    @Autowired
    private IRoomDao roomDao;

    //管理员查询迁移列表
    public ResultView<Page> selectMoveList(Integer page, Integer pageSize, Integer buildingId, String studentName) {
        Page<MoveDto> pageInfo = new Page<>(page, pageSize);
        //根据楼宇号查询楼宇的迁移学生
        LambdaQueryWrapper<Move> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Move::getBuildingId, buildingId);
        List<Move> moveList = this.list(lqw);
        List<MoveDto> moveDtoList = moveList.stream().map(item -> {
            MoveDto moveDto = new MoveDto();
            //查询学生用户名称
            LambdaQueryWrapper<Student> stuLqw = new LambdaQueryWrapper<>();
            stuLqw.eq(Student::getStudentId, item.getStudentId())
                    .like(studentName != null, Student::getUserName, studentName);
            Student student = studentDao.selectOne(stuLqw);
            //封装数据
            moveDto.setStudentName(student.getUserName());
            //封装审核状态
            String status = null;
            if (item.getStatus() == 0) {
                status = "未通过";
            } else if (item.getStatus() == 1) {
                status = "通过";
            } else {
                status = "待审核";
            }
            //封装迁移类型
            String type = null;
            if (item.getType() == 0) {
                type = "迁入";
            } else {
                type = "迁出";
            }
            moveDto.setStatus(status);
            moveDto.setType(type);
            moveDto.setMoveTime(item.getMoveTime());
            moveDto.setReason(item.getReason());
            moveDto.setUserName(student.getUserName());
            moveDto.setStudentName(student.getRealName());
            return moveDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(moveDtoList);
        return ResultView.success(pageInfo);
    }

    //学生查询迁移列表
    public ResultView<Page> selectMoveList(Integer page, Integer pageSize) {
        Page<MoveDto> pageInfo = new Page<>(page, pageSize);
        //获取SecurityContexHolder中的用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long studentId = loginUser.getStudent().getStudentId();
        //通过学生id查询迁移列表
        LambdaQueryWrapper<Move> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Move::getStudentId, studentId);
        List<Move> moveList = this.list(lqw);
        //封装数据
        List<MoveDto> moveDtoList = moveList.stream().map(item -> {
            MoveDto moveDto = new MoveDto();
            Student student = studentDao.selectById(studentId);
            //封装审核状态
            String status = null;
            if (item.getStatus() == 0) {
                status = "未通过";
            } else if (item.getStatus() == 1) {
                status = "通过";
            } else {
                status = "待审核";
            }
            //封装迁移类型
            String type = null;
            if (item.getType() == 0) {
                type = "迁入";
            } else {
                type = "迁出";
            }
            moveDto.setMoveTime(item.getMoveTime());
            moveDto.setStatus(status);
            moveDto.setType(type);
            moveDto.setUserName(student.getUserName());
            moveDto.setStudentName(student.getRealName());
            moveDto.setReason(item.getReason());
            return moveDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(moveDtoList);
        return ResultView.success(pageInfo);
    }

    //新增迁移记录
    @Transactional(rollbackFor = Exception.class)
    public ResultView<String> addMove(Move move) {
        Long studentId = BaseContext.getCurrentId();
        move.setStudentId(studentId);
        //查询buildingId
        Student student = studentDao.selectById(studentId);
        Room room = roomDao.selectById(student.getRoomId());
        move.setBuildingId(room.getBuildingId());
        this.save(move);
        return ResultView.success("提交成功");
    }


    //迁移审核
    public ResultView<String> checkMove(Long moveId,Integer status) {
        LambdaUpdateWrapper<Move> luw = new LambdaUpdateWrapper<>();
        luw.eq(Move::getMoveId,moveId)
                .set(Move::getStatus,status);
        this.update(luw);
        return ResultView.success("审核成功");
    }
    
}
